<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>React生命周期(新)</title>
  </head>

  <body>
    <!-- 准备好一个区域 -->
    <div id="test"></div>

    <script
      type="text/javascript"
      src="../js/17.0.1/react.development.js"
    ></script>
    <script
      type="text/javascript"
      src="../js/17.0.1/react-dom.development.js"
    ></script>
    <script type="text/javascript" src="../js/17.0.1/babel.min.js"></script>
    <script type="text/javascript" src="../js/17.0.1/prop-types.js"></script>
    <script type="text/babel">
      // 创建组件
      class Count extends React.Component {
        // 构造器
        constructor(props) {
          console.log("constructor");
          super(props);
          this.state = { count: 0 };
        }

        //点击加加  事件
        add = () => {
          let { count } = this.state;
          this.setState({ count: (count += 1) });
        };

        // 点击卸载组件 事件
        unadd = () => {
          ReactDOM.unmountComponentAtNode(document.getElementById("test"));
        };

        // 强制更新 事件
        force = () => {
          this.forceUpdate();
        };

        getDerivedStateFromProps() {
          console.log("getDerivedStateFromProps");
        }

        // 组件挂载完毕的钩子
        componentDidMount() {
          console.log("componentDidMount");
        }

        // 组件将要卸载的钩子
        componentWillUnmount() {
          console.log("componentWillunMount");
        }

        // 控制组件更新的【阀门】
        shouldComponentUpdate() {
          console.log("shouldComponentUpdate");
          return false;
        }

        // 组件更新完毕的钩子
        componentDidUpdate() {
          console.log("componentDidUpdate");
        }

        //初始化渲染,状态更新之后
        render() {
          console.log("render");
          return (
            <div>
              <h2>当前求和为{this.state.count}</h2>
              <button onClick={this.add}>点我+1</button>
              <button onClick={this.unadd}>卸载组件</button>
              <button onClick={this.force}>点我强制更新</button>
            </div>
          );
        }
      }

      ReactDOM.render(<Count />, document.getElementById("test"));
    </script>
  </body>
</html>
